<?php

namespace app\model\exam;

use app\model\BaseModel;
use think\App;
use think\model\concern\SoftDelete;

class ExamQuestion extends BaseModel
{
    use SoftDelete;

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $table = 'free_exam_question';
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $iv = '1635230227540WZH';
    protected $encrypt_key = 'Ambition_JnmYsLP';


    public function getDanAttr($value)
    {
        if ($value) return $this->encrypt($value, $this->encrypt_key, $this->iv);
    }

    public function getDuoAttr($value)
    {
        if ($value) return $this->encrypt($value, $this->encrypt_key, $this->iv);
    }

    public function getPanAttr($value)
    {
        if ($value) return $this->encrypt($value, $this->encrypt_key, $this->iv);
    }

    public function getTianAttr($value)
    {
        if ($value) return $this->encrypt($value, $this->encrypt_key, $this->iv);
    }

    public function getContentAttr($value)
    {
        if ($value) return $this->encrypt($value, $this->encrypt_key, $this->iv);
    }

    public function getJsonAttr($value)
    {
        if ($value) return $this->encrypt($value, $this->encrypt_key, $this->iv);

    }

    /**
     * 加密字符串
     * @param string $data 字符串
     * @param string $key 加密key
     * @param string $iv 加密向量
     * @return string
     */
    public function encrypt($data, $key, $iv)
    {
        return base64_encode(openssl_encrypt($data, "AES-128-CBC", $key, true, $iv));
    }

    public function getPatternAttr($value)
    {
        $status = [1 => '考试', 0 => '作业'];
        return $status[$value];
    }

    public function setPatternAttr($value)
    {
        $status = ['考试' => 1, '作业' => 0];
        return $status[$value];
    }

    public function searchPatternAttr($query, $value)
    {
        if (isset($value)) $query->where('pattern', 'like', '%' . $value . '%');

    }

    public function searchTypeAttr($query, $value)
    {
        if ($value) $query->where('type', $value);
    }

    public function searchTitleAttr($query, $value)
    {
        $query->where('title', 'like', '%' . $value . '%');
    }

    public function searchPaperNameAttr($query, $value)
    {
        $query->where('paper_name', 'like', '%' . $value . '%');
    }


}